package com.swt.controller.W_controller;

import com.alibaba.fastjson.JSON;
import com.swt.domain.Image;
import com.swt.service.W_service.ImageService;
import com.swt.util.GetCurrentTime;
import com.swt.util.SaveFileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ImagesController")
public class ImagesController {
    @Resource
    private ImageService imageService;

    @RequestMapping("/filesUpload")
    //requestParam要写才知道是前台的那个数组
    public String filesUpload(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("收到图片!");
        MultipartHttpServletRequest Murequest = (MultipartHttpServletRequest)request;

        //得到文件map对象
        List<String> list = new ArrayList<String>();
        Map<String, MultipartFile> files = Murequest.getFileMap();
        for (MultipartFile file : files.values()) {
            list = SaveFileUtil.saveFile(request, file, list);
        }

        for (int i = 0; i < list.size(); i++) {
            Image image=new Image();
            image.setImagePath(list.get(i));
            image.setImageStatus("待发布");
            image.setImageUpdatetime(GetCurrentTime.getTime());
            imageService.saveImage(image);
            System.out.println("集合里面的数据" + list.get(i));
        }
        return "redirect:/background/picture-list.html";//跳转的页面
    }



    /**
     * 根据时间查询图片
     * */
    @RequestMapping("/listToAll")
    @ResponseBody
    public String listToAll(String page,String logmin,String logmax){
        if(logmin==""&&logmax==""){
            List<Image>list=imageService.listToAll(Integer.parseInt(page));
            System.out.println(list);
            return JSON.toJSONString(list);
        }else {
            List<Image>list=imageService.search(logmin,logmax);
            return JSON.toJSONString(list);
        }
    }

    /**
     * 删除图片
     * */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Image image){
        imageService.delete(image);
        return JSON.toJSONString("true");
    }

    /**
     * 改变图片状态
     * */
    @RequestMapping("/changeStatus")
    @ResponseBody
    public String changeStatus(Image image){
        System.out.println("图片状态："+image.getImageStatus());
        if(Integer.valueOf(imageService.count())<5){
            image.setImageUpdatetime(GetCurrentTime.getTime());
            System.out.println(image.getImageId()+"*******"+image.getImagePath());
            imageService.changeStatus(image);
            return JSON.toJSONString("true");
        }else if(Integer.valueOf(imageService.count())==5&&image.getImageStatus().equals("已下架")){
            imageService.changeStatus(image);
            return JSON.toJSONString("true");
        }
        return JSON.toJSONString("false");
    }


}


